<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <!-- 使用双向绑定的方式获取查询的参数 -->
          <el-input v-model="filters.keywords" placeholder="输入字典明细名称查询"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="dictionaryDetailQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <!--
       data: 表单中的数据绑定
       v-loading: 忙等框
       selection-change: 用于表中复选框选择的触发的事件，做批量操作
       DictionaryDetails：页面加载完后发送请求由后台查询出数据响应到前台 []

      -->
    <el-table :data="DictionaryDetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>




      <el-table-column prop="name" label="字典明细名称" width="150" sortable>
      </el-table-column>

      <el-table-column prop="type.name" label="字典明细类型" width="150" sortable>
      </el-table-column>




      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!--   disabled: 使按钮失效   -->
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     style="float:right;">

      </el-pagination>
    </el-col>

    <!--编辑界面
      rules: 表单验证，用于判断用户是否输入数据
      ref： 点击提交按钮时，再次验证是否输入数据 - 为当前模态框取名

    -->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">

      <el-form  :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">


        <el-form-item label="字典明细名称" prop="name" label-width="110px" >
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>


        <el-form-item label="字典明细类型" prop="types_id" label-width="110px">
          <el-select v-model="editForm.types_id" placeholder="请选择" clearable>

            <el-option
                v-for="item in dictionaryType"
                :key="item.id"
                :label="item.name"
                :value="item.id">

              <!--下拉单中显示的内容-->
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
            </el-option>
          </el-select>

        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>

    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {

      dictionaryType: [],
      title: "",
      filters: {
        keywords: ''
      },
      DictionaryDetails: [],
      totals: 0,
      currentPage: 1,
      pageSize: 5,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        /**
         sn name
         */

        /*types_id: [
          { required: true, message: '请输入字典明细类型', trigger: 'change' }
        ],*/
        name: [
          { required: true, message: '请输入字典明细名称', trigger: 'blur' }
        ],

      },
      //编辑界面数据
      editForm: {
        id: null,
        name: '',
        types_id: null,

      },

    }
  },
  methods: {
    //性别显示转换
    /*formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
    },*/
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDictionaryDetails();
    },


    //查询在模态框中显示的数据字典字典明细类型的数据
    getDictionaryTypes(){
      this.$http.get("/dictionaryType").then(res => this.dictionaryType = res.data)
    },


    /**
     * 问题：当在非第一页查询时，如果条件查询的结果没有超过一页数据，则会显示空屏，
     * 解决：条件查询配合分页查询，显示第一页的数据
     */
    dictionaryDetailQuery(){
      this.currentPage  = 1;
      this.getDictionaryDetails();
    },

    //获取数据列表
    getDictionaryDetails() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keywords: this.filters.keywords
      };
      this.listLoading = true;


      //发送异步请求获取数据
      this.$http.put("/dictionaryDetail", para)
          .then(res => {
            //alert("success")
            //关闭忙等框
            this.listLoading = false;
            this.totals = res.data.totals;
            this.DictionaryDetails = res.data.data;
          }).catch(res => alert("fail!"));
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        //提示框
        type: 'warning'
      }).then(() => {
        //当点击确认时执行

        //忙等框
        this.listLoading = true;

        //获取数据发送请求
        this.$http.delete("/dictionaryDetail/" + row.id)
            .then(res => {
              //关闭忙等框
              if(res.data.success){
                //成功删除
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }else{
                //删除失败
                this.$message({
                  message: res.data.message,
                  type: 'error'
                });
              }

              //操作完成后，刷新页面
              //当删除完当前页面的最后一条数据后，可以就显示空页面
              //或者每次删除完就重新显示第一页数据
              // this.currentPage = 1;
              this.getDictionaryDetails();

            }).catch(res => {
          //发生异常或错误时执行
          this.$message({
            message: '系统异常，稍后重试',
            type: 'error'
          });

        });

        //NProgress.start();
        /*let para = { id: row.id };
        removeDictionaryDetail(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getDictionaryDetails();
        });*/
      }).catch(() => {
        //点击取消时执行
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "编辑";

      //查询在模态框中显示的数据字典字典明细类型的数据
      this.getDictionaryTypes();

      this.editFormVisible = true;

      //通过克隆方式获取回显数据
      this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      //修改标题
      this.title = "增加";

      //查询在模态框中显示的数据字典字典明细类型的数据
      this.getDictionaryTypes();

      //清空模态框
      this.editForm = {
        id: null,
        name: '',
        types_id: null,
      },

          this.editFormVisible = true;

    },
    //编辑页面的提交按钮的事件绑定函数
    editSubmit: function () {

      //this.$refs.editForm 表单的验证
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();

            //通过克隆，获取一份数据，因为使用的是双向绑定
            let para = Object.assign({}, this.editForm);

            //发送请求
            this.$http.post("/dictionaryDetail", para)
                .then(res => {
                  //关闭忙等框
                  this.editLoading = false;


                  if(res.data.success) {
                    //操作成功

                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });


                  }else {
                    //操作失败

                    this.$message({
                      message: res.data.message,
                      type: 'error'
                    });
                  }

                  //关闭模态框
                  this.editFormVisible = false;

                  //刷新页面
                  this.getDictionaryDetails();

                }).catch(res => {
              //发生异常

              //关闭忙等框
              this.editLoading = false;
              this.$message({
                message: '网络延时，请稍后重试',
                type: 'error'
              });


              //关闭模态框
              this.editFormVisible = false;

              //刷新页面
              this.getDictionaryDetails();
            })

          });
        }
      });
    },


    selsChange: function (sels) {
      //复选框选择时将选择的数据记录到data数据模型中的sels中
      this.sels = sels;
      console.log(sels);
    },


    //批量删除
    batchRemove: function () {

      //map 数组的方法映射
      var ids = this.sels.map(item => item.id);


      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        /*let para = { ids: ids };
        batchRemoveDictionaryDetail(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getDictionaryDetails();
        });*/

        //发送请求
        this.$http.patch("/dictionaryDetail", ids).then((res) => {
          this.listLoading = false;

          if(res.data.success) {
            //success
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }

          //操作成功后刷新页面，可以继续查询当前页的内容,即使当前页的数据被删除完
          this.getDictionaryDetails();

        }).catch(res =>{
          //当产生异常时
          this.listLoading = false;
          this.$message({
            message: '网络延迟，请稍后重试！',
            type: 'error'
          });
        });
      }).catch(() => {
        //点击取消按钮
      });
    }
  },
  mounted() {
    this.getDictionaryDetails();
  }
}

</script>

<style scoped>

</style>
